package com.wdcg.view;

import com.wdcg.R;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;

/**
 * @author Grzegorz Matyszczak<BR>
 *
 */
public class RadarBackgroundView extends View {

	public static final float BACKGROUND_LINE_SPACE = 0.1f;

	private Paint backgroundPaint;
	private Paint backgroundLinePaint;
	private Paint radarLinePaint;
	private Paint circleBackgroundPaint;
	private Paint textPaint;

	private int maxDistance;
	
	/**
	 * @param context
	 */
	public RadarBackgroundView(Context context) {
		super(context);
		init();
	}

	/**
	 * @param context
	 * @param attrs
	 */
	public RadarBackgroundView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	/**
	 * @param context
	 * @param attrs
	 * @param defStyle
	 */
	public RadarBackgroundView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {
		backgroundPaint = new Paint();
		backgroundPaint.setColor(getResources().getColor(R.color.radar_background));
		backgroundPaint.setStyle(Style.FILL);

		backgroundLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		backgroundLinePaint.setColor(getResources().getColor(R.color.radar_background_line));

		radarLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		radarLinePaint.setColor(getResources().getColor(R.color.radar_line));
		radarLinePaint.setStrokeWidth(2);
		radarLinePaint.setStyle(Style.STROKE);

		circleBackgroundPaint = new Paint();
		circleBackgroundPaint.setColor(getResources().getColor(R.color.radar_circle_background));
		circleBackgroundPaint.setStyle(Style.FILL);

		textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		textPaint.setColor(getResources().getColor(R.color.radar_line));
		textPaint.setTextSize(12);
		
		//get radar range from conf.xml
		maxDistance = getResources().getInteger(R.integer.radar_background_range);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		//background
		canvas.drawRect(0,0, canvas.getWidth(), canvas.getHeight(), backgroundPaint);

		int lineSpace = (int) (canvas.getWidth() < canvas.getHeight() ? canvas.getWidth()*BACKGROUND_LINE_SPACE : canvas.getHeight()*BACKGROUND_LINE_SPACE);
		
		//horizontal lines
		for(int i=canvas.getHeight()/2+lineSpace; i<canvas.getHeight(); i+=lineSpace)
			canvas.drawLine(0, i, canvas.getWidth(), i, backgroundLinePaint);
		for(int i=canvas.getHeight()/2-lineSpace; i>0; i-=lineSpace)
			canvas.drawLine(0, i, canvas.getWidth(), i, backgroundLinePaint);

		//vertical lines
		for(int i=canvas.getWidth()/2+lineSpace; i<canvas.getWidth(); i+=lineSpace)
			canvas.drawLine(i, 0, i, canvas.getHeight(), backgroundLinePaint);
		for(int i=canvas.getWidth()/2-lineSpace; i>0; i-=lineSpace)
			canvas.drawLine(i, 0, i, canvas.getHeight(), backgroundLinePaint);

		//middle horizontal line
		canvas.drawLine(0, canvas.getHeight()/2, canvas.getWidth(), canvas.getHeight()/2, radarLinePaint);

		//middle vertical line
		canvas.drawLine(canvas.getWidth()/2, 0, canvas.getWidth()/2, canvas.getHeight(), radarLinePaint);

		//circles number
		float numbers = 0.5f;
		while((numbers+1)*lineSpace <= (canvas.getWidth() < canvas.getHeight() ? canvas.getWidth()/2 : canvas.getHeight()/2)*9/10)
			numbers++;

		//circle background
		float radius = numbers*lineSpace;
		canvas.drawCircle(canvas.getWidth()/2, canvas.getHeight()/2, radius, circleBackgroundPaint);

		//radar circles
		for(float i=0.5f; i<=numbers; i++) {
			canvas.drawCircle(canvas.getWidth()/2, canvas.getHeight()/2, i*lineSpace, radarLinePaint);
			String text = (int)(i*maxDistance/numbers)+"m";
			canvas.drawText(text, canvas.getWidth()/2+2, (int)(canvas.getHeight()/2-i*lineSpace)-2, textPaint);
		}
	}


}
